import 'package:flutter/material.dart';
import 'package:flutter_quick/pages/friend/friend.dart';
import 'package:flutter_quick/pages/find/find.dart';
import 'package:flutter_quick/pages/home/home.dart';
import 'package:flutter_quick/pages/my/my.dart';

class AppFrame extends StatefulWidget {
  AppFrame({Key? key}) : super(key: key);

  @override
  _AppFrameState createState() => _AppFrameState();
}

class _AppFrameState extends State<AppFrame>
    with SingleTickerProviderStateMixin {
  int _tabIdx = 0;
  late TabController _tabController;
  final List<TabItem> _tabs = <TabItem>[
    TabItem(
        text: '聊天',
        page: const HomePage(),
        icon: Icons.chat_bubble,
        unselectedIcon: Icons.chat_bubble_outline),
    TabItem(
        text: '通讯录',
        page: const friendPage(),
        icon: Icons.person_add_alt_1,
        unselectedIcon: Icons.person_add_alt),
    TabItem(
        text: '发现',
        page: const FindPage(),
        icon: Icons.filter_tilt_shift,
        unselectedIcon: Icons.filter_tilt_shift),
    TabItem(
        text: '我',
        page: const MyPage(),
        icon: Icons.person,
        unselectedIcon: Icons.person_outline_outlined),
  ];

  @override
  void initState() {
    super.initState();

    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _tabs.length,
      child: Scaffold(
        bottomNavigationBar: Theme(
          data: ThemeData(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent),
          child: TabBar(
            onTap: (idx) {
              setState(() {
                _tabIdx = idx;
              });
            },
            labelColor: Color(0xff58be6b),
            controller: _tabController,
            indicator: BoxDecoration(),
            unselectedLabelColor: Colors.black,
            labelStyle: TextStyle(
              fontSize: 10,
            ),
            tabs: _tabs.map((e) {
              int idx = _tabs.indexOf(e);

              return Tab(
                icon: Icon(
                  // e.unselectedIcon,
                  idx == _tabIdx ? e.icon : e.unselectedIcon,
                  size: 22,
                ),
                text: e.text,
                height: 40,
                iconMargin: EdgeInsets.only(bottom: 0),
              );
            }).toList(),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: _tabs.map((e) => e.page).toList(),
        ),
      ),
    );
  }
}

class TabItem {
  String text;
  Widget page;
  IconData icon;
  IconData unselectedIcon;

  TabItem(
      {required this.text,
      required this.page,
      required this.icon,
      required this.unselectedIcon});
}
